﻿--[[
   @brief    ui logic for image_viewer ui
   @author   amadeusyu
   @version  1.0
   @date     2017-03-23
--]]

IMAGEVIEWER_ERRCODE_SUCCESS = 0;
IMAGEVIEWER_ERRCODE_FAILED = 1;

image_viewer_control = {
    data = nil;
    download_task_id = -1;
    image_local_path = "";
    wallpaper_req = "";
    browser_id = 0;
    init = function()
        local tpf_ui = base_utility.get_component_adapter("ITpf_ui", "ierd_tgp.tpf_ui.GetTPFUI();");
        tpf_ui:RegisterJsEvent("jc_open_image_viewer", "image_viewer_control.open_image_viewer");
        tpf_ui:RegisterJsEvent("jc_get_image_viewer_data", "image_viewer_control.send_images");
        tpf_ui:RegisterJsEvent("jc_resize_image_viewer", "image_viewer_control.resize");
        tpf_ui:RegisterJsEvent("jc_maximize_image_viewer", "image_viewer_control.maximize");
        tpf_ui:RegisterJsEvent("jc_restore_image_viewer", "image_viewer_control.restore");
        tpf_ui:RegisterJsEvent("jc_set_wallpaper", "image_viewer_control.set_wallpaper");
        tpf_ui:AddLuaListener(ierd_tgp.upd_loader.kOnDownloadResult,
            "image_viewer_control.on_download_result");
    end;
    
    on_login_success = function()
    end;
    
    open_image_viewer = function(func_name, json, browser_id)
        ierd_tgp.tpf_ui.Require("image_viewer\\view.lua");
        local info = base_utility.json_to_table(json);
        if info == nil or info.data ==nil then
            image_viewer_control.reply_js_call(browser_id, json, "", IMAGEVIEWER_ERRCODE_FAILED,
                "invalid data");
            return
        end
        if image_viewer_control.is_browser_exist() == false then
            image_viewer_view.create_browser();
        end
        image_viewer_control.data = info.data;
        image_viewer_view.navigate(info.data.url, info.data.width, info.data.height);
        image_viewer_control.reply_js_call(browser_id, json, "", IMAGEVIEWER_ERRCODE_SUCCESS, "");
    end;

    send_images = function(func_name, json, browser_id)
        image_viewer_control.reply_js_call(browser_id, json, image_viewer_control.data,
            IMAGEVIEWER_ERRCODE_SUCCESS,
            "");
    end;
    
    resize = function(func_name, json, browser_id)
        local info = base_utility.json_to_table(json);
        if info == nil or info.data ==nil then
            return
        end
        image_viewer_view.fit_size(info.data.width, info.data.height);
        image_viewer_control.reply_js_call(browser_id, json, "", IMAGEVIEWER_ERRCODE_SUCCESS, "");
        image_viewer_view.show(1);
    end;
    
    maximize = function(func_name, json, browser_id)
        image_viewer_view.maximize();
        image_viewer_control.reply_js_call(browser_id, json, "", IMAGEVIEWER_ERRCODE_SUCCESS, "");
    end;
    
    restore = function(func_name, json, browser_id)
        image_viewer_view.restore();
        image_viewer_control.reply_js_call(browser_id, json, "", IMAGEVIEWER_ERRCODE_SUCCESS, "");
    end;
    
    set_wallpaper = function(func_name, json, browser_id)
        image_viewer_control.browser_id = browser_id;
        local info = base_utility.json_to_table(json);
        if info == nil or info.data ==nil then
            return
        end
        base_utility.log("[image_viewer]set_wallpaper:" .. info.data.url);
        local download_mgr = base_utility.get_component_adapter("IDownload_mgr",
            "ierd_tgp.upd_loader.GetDownloadMgrModule()");
        local path = base_utility.get_work_path();
        _,_,name = string.find(info.data.url, "/([^/]+)$");
        if name ~= nil then
            base_utility.log("[image_viewer]img name:" .. name);
            path = path .. "\\tpf_ui\\res\\dl\\" .. name;
            image_viewer_control.image_local_path = path;
            image_viewer_control.download_task_id = download_mgr:download(info.data.url, path, 0,
                                             ierd_tgp.upd_loader.Download_channelPOD.PIC);
            image_viewer_control.wallpaper_req = json;
        else
            base_utility.log("[image_viewer]parse name failed!");
            image_viewer_control.reply_js_call(browser_id, json, "", IMAGEVIEWER_ERRCODE_FAILED,
                "parse name failed");
        end
    end;    
    
    on_download_result = function(event_id, json)
        local info = base_utility.json_to_table(json);
        if info == nil then
            return
        end
        if info["fake_task_id"] == image_viewer_control.download_task_id then
            if info["error_code"] == 0 then
                base_utility.log("[image_viewer]wallpaper download success!");
                if ierd_tgp.common.SetWallpaper(image_viewer_control.image_local_path) then
                    image_viewer_control.reply_js_call(image_viewer_control.browser_id,
                        image_viewer_control.wallpaper_req,
                        "",
                        IMAGEVIEWER_ERRCODE_SUCCESS,
                        "");
                else
                    image_viewer_control.reply_js_call(image_viewer_control.browser_id,
                        image_viewer_control.wallpaper_req,
                        "",
                        IMAGEVIEWER_ERRCODE_FAILED,
                        "SetWallpaper failed");
                end
            else
                base_utility.log("[image_viewer]wallpaper download failed!");
                image_viewer_control.reply_js_call(image_viewer_control.browser_id,
                    image_viewer_control.wallpaper_req,
                    "", IMAGEVIEWER_ERRCODE_FAILED, "download failed");
            end
        end
    end;
    
    reply_js_call = function(browser_id, req_json, reply_data, error_code, msg)
        local param = base_utility.json_to_table(req_json);
        if nil ~= param then
            local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
            rsp_table.data = reply_data;
            rsp_table.retCode = error_code;
            rsp_table.msg = msg;
            if param.callback_func ~= nil and param.callback_func ~= "" then
                local rsp_json = base_utility.table_to_json(rsp_table);
                web_utility.lua_call_js_by_id_ex(param.callback_func, rsp_json, browser_id);
            end
        end
    end;
    
    is_browser_exist = function()
        local component = ui_utility.get_ctrl_ptr_by_cid(CID_IMAGEVIEWER_DLG);
        if component == 0 or component == nil then
          return false;
        end
        return true;
    end;
};
image_viewer_control.init();
